package com.wwp.tbshop.activity.orderlist;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.wwp.tbshop.R;
import com.wwp.tbshop.dagger2.DaggerOrderListActivityComponent;
import com.wwp.tbshop.dagger2.OrderListAvtivityModule;
import com.wwp.tbshop.model.bean.OrderListItemsBean;
import com.wwp.tbshop.model.bean.OrderListItemsBean.OrderListBean;
import com.wwp.tbshop.presenter.OrderListOperation;
import com.wwp.tbshop.util.SPUtils;
import com.wwp.tbshop.view.adapter.OrderListAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class OrderListActivity extends Activity {

    private static final String TAG = "MainActivity";

    @Bind(R.id.btn_recent)
    Button mBtnRecent;
    @Bind(R.id.btn_before)
    Button mBtnBefore;
    @Bind(R.id.btn_canceled)
    Button mBtnCanceled;
    @Bind(R.id.list_lv)
    ListView mListLv;

    private int listType = 0;

    // private OrderListOperation mOrderListOperation;

    @Inject
    OrderListOperation mOrderListOperation;



    private int mUserId = 20428;//20428;
    private OrderListAdapter mOrderListAdapter;

    // protected ActionBar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // TODO: 2017/6/17  从缓存中获取userId
        mUserId = Integer.parseInt( SPUtils.getString(this, SPUtils.USERID, "0"));
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);

      /*  mActionBar = getSupportActionBar();
        mActionBar.setTitle("订单详情");
        mActionBar.setDisplayHomeAsUpEnabled(true);// 设置back按钮是否可见*/

        //mOrderListOperation = new_goods OrderListOperation(this);
        DaggerOrderListActivityComponent.builder().orderListAvtivityModule(new OrderListAvtivityModule(this)).build().in(this);


       getOrderList(1);
    }

    public void recent(View v) {
      getOrderList(1);
    }

    public void before(View v) {
      getOrderList(2);
    }

    public void canceled(View v) {
       getOrderList(3);
    }

    public void upDateOrderList(List<OrderListBean> orderList, int type) {
        mOrderListAdapter = new OrderListAdapter(this, orderList, type);
        mListLv.setAdapter(mOrderListAdapter);
    }

   /* @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        finish();
        return true;
    }*/

    @Override
    protected void onStart() {
        super.onStart();
        getOrderList(listType);
    }

    private void getOrderList(int type){
        mOrderListOperation.getOrderList(mUserId, type, 0, 100);
        listType = type;
        mBtnRecent.setEnabled(true);
        mBtnBefore.setEnabled(true);
        mBtnCanceled.setEnabled(true);
        switch (type){
            case 1:
                mBtnRecent.setEnabled(false);
                break;
            case 2:
                mBtnBefore.setEnabled(false);
                break;
            case 3:
                mBtnCanceled.setEnabled(false);
                break;
        }
    }

    public void processError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();

    }

    public void setData(OrderListItemsBean orderListItemsBean, int type) {
        mOrderListAdapter.setData(orderListItemsBean,type);
    }
}
